package yxy.game.pm2.active.module.subordinateclass.rankrush;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.acttaskranking;
import yxy.cherry.data.bean.task;
import yxy.game.pm2.active.module.ActOpenHandle;
import yxy.game.pm2.active.module.OpenType;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.module.rank.active.RankRushEnum;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.bean.timeline.Timeline;
import yxy.game.pm2.bean.timeline.TimelineModule;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.HashMap;
import java.util.Map;

public class RankRush extends TimelineModule {

	//bean ------------------------------------------------------------------
	private int type;	//类型 标明是哪个冲榜
	private HashMap<Integer, cbtask> idtaskcodemap = new HashMap<Integer, cbtask>();		//key : 表格id value ：任务code
	
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public HashMap<Integer, cbtask> getIdtaskcodemap() {
		return idtaskcodemap;
	}
	public void setIdtaskcodemap(HashMap<Integer, cbtask> idtaskcodemap) {
		this.idtaskcodemap = idtaskcodemap;
	}

	//func --------------------------------------------------------------------
	
	/**
	 * 刷新
	 * @param tl
	 */
	public void refresh(Timeline tl) {

	}

	/**
	 * @param pcode
	 */
	public static RankRush create(String pcode, int acttype) {
		if(RankRushEnum.getRankRushEnum(acttype) == null) {
			return null;
		}
		
		PlayerBean pb = PlayerBean.get(pcode);
		int sid = pb.getServerId();
		
		ServerBean sb = ServerBean.get(sid);
		int areaid = sb.getAreaId();
		
		activeControl sai = activeControl.get(areaid, acttype);
		if(sai == null) {
			return null;
		}
		
		RankRush rr = new RankRush();
		rr.setTimeline(sai.getTimeline());
		rr.setType(acttype);
		
		//活动进行中才创建任务
		if(sai.running()) {
			Map<String, acttaskranking> datamap = ActDataCenter.getAllData(acttaskranking.class, areaid);
			for(acttaskranking data : datamap.values()) {
				if(data.getGroup() == acttype) {
					int taskid = data.getTaskid();
					task taskdata = DataCenter.getData(taskid, task.class);
					if(taskdata == null) {
						continue;
					}
					
					Task taskbean = Task.create(pcode, taskdata);
					cbtask ct = new cbtask(data.getId(), taskid, taskbean.getCode());
					rr.getIdtaskcodemap().put(ct.getId(), ct);
				}
			}
		}
		
		return rr;
	}

	/** 查看是否有可领取奖励 控制红点 */
	public void check(String playercode) {
		if(running()) {
			new ActOpenHandle().updateActOpen(playercode, getType(), OpenType.开启);
		}else {
			new ActOpenHandle().updateActOpen(playercode, getType(), OpenType.关闭);
		}
		
		TipType tt = getRankRushTypeType(getType());
		if(tt == null) {
			return;
		}
		
		if(running()) {
			for (cbtask ct : getIdtaskcodemap().values()) {
				String taskcode = ct.getTaskcode();
				Task taskbean = Task.get(playercode, taskcode);
				if (taskbean == null) {
					continue;
				}
				
				if (taskbean.isReward()) {
					continue;
				}
				
				if (taskbean.isFinish() && !taskbean.isReward()) {
					new TipHandle().updateTip(playercode, tt, true);
					return;
				}
			}
		}

		new TipHandle().updateTip(playercode, tt, false);
	}
	
	/**
	 * 获取冲榜红点类型
	 * @param type
	 * @return
	 */
	private TipType getRankRushTypeType(int type) {
		if(type == RankRushEnum.冲榜_副本关卡.getType()) {
			return TipType.活动_冲榜副本关卡奖励;
		}
		
		if(type == RankRushEnum.冲榜_剿匪次数.getType()) {
			return TipType.活动_冲榜剿匪次数奖励;
		}
		
		if(type == RankRushEnum.冲榜_快速作战.getType()) {
			return TipType.活动_冲榜快速作战奖励;
		}
		
		if(type == RankRushEnum.冲榜_金币兑换.getType()) {
			return TipType.活动_冲榜金币兑换奖励;
		}
		
		return null;
	}
	
	@Override
	public String lockKey() {
		return null;
	}



}
